package com.ccnf.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ccnf.core.exception.BizException;
import com.ccnf.core.utils.ResultUtil;
import com.ccnf.model.entity.Invitation;
import com.ccnf.model.entity.InvitationAuth;
import com.ccnf.model.mapper.InvitationAuthMapper;
import com.ccnf.model.mapper.InvitationMapper;
import com.ccnf.service.InvitationAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author developer
 * @since 2018-08-03
 */
@Service
public class InvitationAuthServiceImpl extends ServiceImpl<InvitationAuthMapper, InvitationAuth> implements InvitationAuthService {
    @Autowired
    private InvitationMapper invitationMapper;

    @Override
    public Long saveInvitationAuth(InvitationAuth activityAuth) {
        ResultUtil.validateSQL(baseMapper.insert(activityAuth), "录入活动授权信息失败");
        return activityAuth.getAuthId();
    }

    @Override
    public void modifyInvitationAuth(InvitationAuth activityAuth) {
        InvitationAuth selectInvitationAuth = baseMapper.selectById(activityAuth.getAuthId());
        ResultUtil.validateNull(selectInvitationAuth, "授权信息不存在");
        Long selectUserId = selectInvitationAuth.getUserId();
        Long modifyUserId = activityAuth.getUserId();
        if (selectUserId == 0) {
            InvitationAuth select = new InvitationAuth();
            select.setInvitationId(selectInvitationAuth.getInvitationId());
            select.setUserId(modifyUserId);
            if (baseMapper.selectOne(select) != null) {
                throw new BizException("你已经成功授权该活动管理人");
            }
            ResultUtil.validateSQL(baseMapper.updateById(activityAuth), "活动授权信息修改失败");
        } else {
            if (modifyUserId != null) {
                if (modifyUserId.equals(selectUserId)) {
                    throw new BizException("你已经成功授权该活动管理人");
                } else {
                    throw new BizException("该授权链接已存在授权人信息");
                }
            }
        }
    }

    @Override
    public List<Map<String, Object>> findInvitationAuthUsersByInvitationId(Long invitationId) {
        return baseMapper.selectInvitationAuthUsersByInvitationId(invitationId);
    }

    @Override
    public void removeAuthUsers(Long invitationId, Long userId, Long[] authIds) {
        Invitation activity = invitationMapper.selectBasicByInvitationId(invitationId);
        ResultUtil.validateNull(activity, "活动信息为空");
        if (!activity.getUserId().equals(userId)) {
            throw new BizException("你没有权限删除");
        }
        for (Long authId: authIds) {
            InvitationAuth activityAuth = baseMapper.selectById(authId);
            if (activityAuth != null) {
                if (!activityAuth.getInvitationId().equals(invitationId)) {
                    throw new BizException("你没有权限删除");
                }
                ResultUtil.validateSQL(baseMapper.deleteById(authId), "删除失败");
            }
        }
    }

    @Override
    public InvitationAuth findInvitationAuthByInvitationIdAndUserId(Long invitationId, Long userId) {
        InvitationAuth activityAuth = new InvitationAuth();
        activityAuth.setInvitationId(invitationId);
        activityAuth.setUserId(userId);
        return baseMapper.selectOne(activityAuth);
    }
}